<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000">
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top">
  
  
  <title>Docker教程 | 王竹兴 | Blob</title>
  <meta name="description" content="概述  Docker 是一个开源的应用容器引擎，基于Go 语言 并遵从Apache2.0协议开源。 Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。 容器是完全使用沙箱机制，相互之间不会有任何接口（类似 iPhone 的 app）,更重要的是容器性能开销极低。    应用场景  Web 应用的自动化打包">
<meta name="keywords" content="持续集成">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker教程">
<meta property="og:url" content="http://zhulao.gitee.io/blog/2019/04/20/Docker教程/index.html">
<meta property="og:site_name" content="王竹兴 | Blog">
<meta property="og:description" content="概述  Docker 是一个开源的应用容器引擎，基于Go 语言 并遵从Apache2.0协议开源。 Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。 容器是完全使用沙箱机制，相互之间不会有任何接口（类似 iPhone 的 app）,更重要的是容器性能开销极低。    应用场景  Web 应用的自动化打包">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/929ea2a1gy1g28w0yg4l4j20ed0awac7.jpg">
<meta property="og:updated_time" content="2019-04-27T14:12:31.663Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Docker教程">
<meta name="twitter:description" content="概述  Docker 是一个开源的应用容器引擎，基于Go 语言 并遵从Apache2.0协议开源。 Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。 容器是完全使用沙箱机制，相互之间不会有任何接口（类似 iPhone 的 app）,更重要的是容器性能开销极低。    应用场景  Web 应用的自动化打包">
<meta name="twitter:image" content="http://ww1.sinaimg.cn/large/929ea2a1gy1g28w0yg4l4j20ed0awac7.jpg">
  <!-- Canonical links -->
  <link rel="canonical" href="http://zhulao.gitee.io/blog/2019/04/20/Docker教程/index.html">
  
    <link rel="alternate" href="/atom.xml" title="王竹兴 | Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  
  
  
</head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/wzx3" target="_blank">
          <img class="img-circle img-rotate" src="/blog/images/avatar.jpeg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">王竹兴</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md"></h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> XiaMen, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索">
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech>
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/blog/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-repository">
          <a href="/blog/repository">
            
            <i class="icon icon-project"></i>
            
            <span class="menu-title">项目</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/wzx3" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="/blog/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>人生最快意的事情之一, 就是你比别人牛逼, 然后静静的看别人在你面前装逼。</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/便签/">便签</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/教程/">教程</a><span class="category-list-count">22</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/游记/">游记</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/随笔/">随笔</a><span class="category-list-count">6</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/blog/tags/DL/" style="font-size: 13.25px;">DL</a> <a href="/blog/tags/Debug/" style="font-size: 13.5px;">Debug</a> <a href="/blog/tags/Doc/" style="font-size: 13px;">Doc</a> <a href="/blog/tags/Hexo/" style="font-size: 13.25px;">Hexo</a> <a href="/blog/tags/Links/" style="font-size: 13px;">Links</a> <a href="/blog/tags/Linux/" style="font-size: 13.25px;">Linux</a> <a href="/blog/tags/Mac/" style="font-size: 13px;">Mac</a> <a href="/blog/tags/bazel/" style="font-size: 13px;">bazel</a> <a href="/blog/tags/c-c/" style="font-size: 13.75px;">c/c++</a> <a href="/blog/tags/git/" style="font-size: 13px;">git</a> <a href="/blog/tags/rst/" style="font-size: 13px;">rst</a> <a href="/blog/tags/scripts/" style="font-size: 13.75px;">scripts</a> <a href="/blog/tags/ssh/" style="font-size: 13px;">ssh</a> <a href="/blog/tags/国学经典/" style="font-size: 13px;">国学经典</a> <a href="/blog/tags/持续集成/" style="font-size: 13.25px;">持续集成</a> <a href="/blog/tags/科幻小说/" style="font-size: 14px;">科幻小说</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/05/">五月 2019</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/04/">四月 2019</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/03/">三月 2019</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/02/">二月 2019</a><span class="archive-list-count">10</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/26/读论语/" class="title">读论语</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-26T01:45:07.000Z" itemprop="datePublished">2019-05-26</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/20/读变型战争/" class="title">读变型战争</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-20T15:51:57.000Z" itemprop="datePublished">2019-05-20</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/20/读星际远征/" class="title">读黑客横行</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-20T15:40:09.000Z" itemprop="datePublished">2019-05-20</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/教程/">教程</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/15/代码格式化工具-clang-format/" class="title">代码格式化工具:clang-format</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-15T14:39:51.000Z" itemprop="datePublished">2019-05-15</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/教程/">教程</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/12/Linux-疑难杂症/" class="title">Linux 疑难杂症</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-12T02:50:29.000Z" itemprop="datePublished">2019-05-12</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<aside class="sidebar sidebar-toc collapse" id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <nav id="toc" class="article-toc">
      <h3 class="toc-title">文章目录</h3>
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#docker-安装"><span class="toc-number">1.</span> <span class="toc-text"> Docker 安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker-使用"><span class="toc-number">2.</span> <span class="toc-text"> Docker 使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#简单使用"><span class="toc-number">2.1.</span> <span class="toc-text"> 简单使用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#hello-world"><span class="toc-number">2.1.1.</span> <span class="toc-text"> Hello World</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#运行交互式容器"><span class="toc-number">2.1.2.</span> <span class="toc-text"> 运行交互式容器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#后台模式运行"><span class="toc-number">2.1.3.</span> <span class="toc-text"> 后台模式运行</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#常用环境部署"><span class="toc-number">2.2.</span> <span class="toc-text"> 常用环境部署</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#容器环境部署"><span class="toc-number">2.2.1.</span> <span class="toc-text"> 容器环境部署</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#容器与宿主机之间的文件复制"><span class="toc-number">2.2.2.</span> <span class="toc-text"> 容器与宿主机之间的文件复制</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#容器与宿主机共享文件"><span class="toc-number">2.2.3.</span> <span class="toc-text"> 容器与宿主机共享文件</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#镜像使用"><span class="toc-number">2.3.</span> <span class="toc-text"> 镜像使用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#列出本地主机上的镜像列表"><span class="toc-number">2.3.1.</span> <span class="toc-text"> 列出本地主机上的镜像列表</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#获取新的镜像"><span class="toc-number">2.3.2.</span> <span class="toc-text"> 获取新的镜像</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#查找远端镜像"><span class="toc-number">2.3.3.</span> <span class="toc-text"> 查找远端镜像</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#更新镜像"><span class="toc-number">2.3.4.</span> <span class="toc-text"> 更新镜像</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#删除镜像"><span class="toc-number">2.3.5.</span> <span class="toc-text"> 删除镜像</span></a></li></ol></li></ol></li></ol>
    </nav>
  </div>
</aside>

<main class="main" role="main">
  <div class="content">
  <article id="post-Docker教程" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      Docker教程
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/blog/2019/04/20/Docker教程/" class="article-date">
	  <time datetime="2019-04-20T02:10:14.000Z" itemprop="datePublished">2019-04-20</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/blog/categories/教程/">教程</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link" href="/blog/tags/持续集成/">持续集成</a>
  </span>


        
	<span class="article-read hidden-xs">
	    <i class="icon icon-eye-fill" aria-hidden="true"></i>
	    <span id="busuanzi_container_page_pv">
			<span id="busuanzi_value_page_pv">0</span>
		</span>
	</span>


        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/blog/2019/04/20/Docker教程/#comments" class="article-comment-link">评论</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 1,475(字)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 6(分)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <ul>
<li>
<p>概述</p>
<ul>
<li><code>Docker</code> 是一个<strong>开源的应用容器引擎</strong>，基于<code>Go 语言</code> 并遵从Apache2.0协议开源。</li>
<li>Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。</li>
<li><strong>容器是完全使用沙箱机制，相互之间不会有任何接口</strong>（类似 iPhone 的 app）,更重要的是<strong>容器性能开销极低</strong>。</li>
</ul>
</li>
<li>
<p>应用场景</p>
<ul>
<li>Web 应用的<strong>自动化打包和发布</strong>。</li>
<li><strong>自动化测试和持续集成、发布</strong>。</li>
<li>在<strong>服务型环境中部署和调整数据库或其他的后台应用</strong>。</li>
</ul>
</li>
<li>
<p>架构模式</p>
<ul>
<li>
<p>Docker 使用<strong>客户端-服务器 (C/S) 架构模式</strong>，使用远程API来管理和创建Docker容器。</p>
</li>
<li>
<p>Docker <strong>容器通过 Docker 镜像来创建</strong>。</p>
</li>
<li>
<p>容器与镜像的关系类似于面向对象编程中的对象与类:</p>
<table>
<thead>
<tr>
<th style="text-align:center">Docker</th>
<th style="text-align:center">面向对象</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">容器</td>
<td style="text-align:center">对象</td>
</tr>
<tr>
<td style="text-align:center">镜像</td>
<td style="text-align:center">类</td>
</tr>
</tbody>
</table>
<p><img src="http://ww1.sinaimg.cn/large/929ea2a1gy1g28w0yg4l4j20ed0awac7.jpg" alt></p>
<table>
<thead>
<tr>
<th style="text-align:left">概念名词</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">Docker 镜像(<strong>Images</strong>)</td>
<td>用于创建 Docker 容器的模板。</td>
</tr>
<tr>
<td style="text-align:left">Docker 容器(<strong>Container</strong>)</td>
<td>独立运行的一个或一组应用。</td>
</tr>
<tr>
<td style="text-align:left">Docker 客户端(<strong>Client</strong>)</td>
<td>通过命令行或其他工具使用 <strong>Docker API</strong> 与 <strong>Docker 的守护进程</strong>通信。</td>
</tr>
<tr>
<td style="text-align:left">Docker 主机(<strong>Host</strong>)</td>
<td>一个物理或者虚拟的机器<strong>用于执行 Docker 守护进程和容器</strong>。</td>
</tr>
<tr>
<td style="text-align:left">Docker 仓库(<strong>Registry</strong>)</td>
<td>用来保存镜像。<a href="https://hub.docker.com" target="_blank" rel="noopener">Docker Hub</a> 提供了庞大的镜像集合供使用。</td>
</tr>
</tbody>
</table>
</li>
</ul>
</li>
<li>
<p>优点</p>
<ul>
<li><strong>简化程序</strong><br>
Docker 让开发者<strong>可以打包他们的应用以及依赖包到一个可移植的容器中</strong>，然后发布到任何流行的 Linux 机器上，便可以实现虚拟化。</li>
<li><strong>避免选择恐惧症</strong><br>
Docker 镜像中包含了运行环境和配置，所以 Docker 可以简化部署多种应用实例工作。比如 Web 应用、后台应用、数据库应用、大数据应用比如 Hadoop 集群、消息队列等等<strong>都可以打包成一个镜像部署</strong>。</li>
<li><strong>节省开支</strong><br>
云计算时代到来，使开发者不必为了追求效果而配置高额的硬件，Docker 改变了高性能必然高价格的思维定势。Docker 与云的结合，让云空间得到更充分的利用。不仅解决了硬件管理的问题，也改变了虚拟化的方式。</li>
</ul>
</li>
<li>
<p>相关链接</p>
<ul>
<li><a href="https://www.docker.com" target="_blank" rel="noopener">Docker 官网</a></li>
<li><a href="https://github.com/docker/docker" target="_blank" rel="noopener">Github Docker 源码</a></li>
<li><a href="http://www.runoob.com/docker/docker-tutorial.htm" target="_blank" rel="noopener">菜鸟教程-Docker</a></li>
</ul>
</li>
</ul>
<h3 id="docker-安装"><a class="markdownIt-Anchor" href="#docker-安装"></a> Docker 安装</h3>
<ul>
<li><a href="https://www.docker-cn.com/community-edition#/download" target="_blank" rel="noopener">下载地址</a></li>
</ul>
<h3 id="docker-使用"><a class="markdownIt-Anchor" href="#docker-使用"></a> Docker 使用</h3>
<h4 id="简单使用"><a class="markdownIt-Anchor" href="#简单使用"></a> 简单使用</h4>
<h5 id="hello-world"><a class="markdownIt-Anchor" href="#hello-world"></a> Hello World</h5>
<blockquote>
<p>使用 <strong>docker run</strong> 命令来在容器内运行一个应用程序：输出Hello world。</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> Docker 以 ubuntu16.04 镜像创建一个新容器，然后在容器里执行并输出结果</span></span><br><span class="line">docker run ubuntu:16.04 echo "Hello World"</span><br></pre></td></tr></table></figure>
<p>第一次拉取, 会检测本地没有ubuntu16.04的镜像, 直接从远端拉取。</p>
<h5 id="运行交互式容器"><a class="markdownIt-Anchor" href="#运行交互式容器"></a> 运行交互式容器</h5>
<blockquote>
<ul>
<li>进入容器进行交互</li>
<li>可以通过运行exit命令或者使用CTRL+D来退出容器</li>
</ul>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> -i:允许你对容器内的标准输入 (STDIN) 进行交互</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> -t:在新容器内指定一个伪终端或终端</span></span><br><span class="line">docker run -i -t ubuntu:16.04</span><br><span class="line">docker run -it ubuntu:16.04</span><br></pre></td></tr></table></figure>
<h5 id="后台模式运行"><a class="markdownIt-Anchor" href="#后台模式运行"></a> 后台模式运行</h5>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 创建一个以进程方式运行的容器</span></span><br><span class="line">docker run -d ubuntu:16.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 确认容器是否有在运行, 获取正在运行的容器ID</span></span><br><span class="line">docker ps</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看容器内的标准输出</span></span><br><span class="line">docker logs [ContainerID]</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 停止容器运行</span></span><br><span class="line">docker stop [ContainerID]</span><br></pre></td></tr></table></figure>
<h4 id="常用环境部署"><a class="markdownIt-Anchor" href="#常用环境部署"></a> 常用环境部署</h4>
<h5 id="容器环境部署"><a class="markdownIt-Anchor" href="#容器环境部署"></a> 容器环境部署</h5>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 配置国内镜像</span></span><br><span class="line">mv /etc/apt/sources.list /etc/apt/sources.list.bak</span><br><span class="line">echo "deb http://mirrors.163.com/debian/ jessie main non-free contrib" &gt;&gt; /etc/apt/sources.list</span><br><span class="line">echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" &gt;&gt; /etc/apt/sources.list</span><br><span class="line">echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib"  &gt;&gt; /etc/apt/sources.list</span><br><span class="line">echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" &gt;&gt; /etc/apt/sources.list</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 更新安装源</span></span><br><span class="line">apt-get update</span><br></pre></td></tr></table></figure>
<h5 id="容器与宿主机之间的文件复制"><a class="markdownIt-Anchor" href="#容器与宿主机之间的文件复制"></a> 容器与宿主机之间的文件复制</h5>
<ul>
<li>
<p>容器往宿主机拷贝</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker cp [ContainerID]：/database_dump_bak/db_bak.dmp /home/test</span><br></pre></td></tr></table></figure>
</li>
<li>
<p>宿主机往容器拷贝</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker cp wenjian_001.xml  [ContainerID]:/mnt/5101388419</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h5 id="容器与宿主机共享文件"><a class="markdownIt-Anchor" href="#容器与宿主机共享文件"></a> 容器与宿主机共享文件</h5>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 进入Docker容器后，进入 /share 文件夹下即可查看到共享内容</span></span><br><span class="line">docker run -it -v /home/wzx/下载:/share wzx/ubuntu:16.04 /bin/bash</span><br></pre></td></tr></table></figure>
<h4 id="镜像使用"><a class="markdownIt-Anchor" href="#镜像使用"></a> 镜像使用</h4>
<h5 id="列出本地主机上的镜像列表"><a class="markdownIt-Anchor" href="#列出本地主机上的镜像列表"></a> 列出本地主机上的镜像列表</h5>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker images</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> e.g:</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> ubuntu              16.04               9361ce633ff1        5 weeks ago         118MB</span></span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li>**REPOSITORY：**表示镜像的仓库源</li>
<li>**TAG：**镜像的标签</li>
<li>**IMAGE ID：**镜像ID</li>
<li>**CREATED：**镜像创建时间</li>
<li>**SIZE：**镜像大小</li>
</ul>
</blockquote>
<h5 id="获取新的镜像"><a class="markdownIt-Anchor" href="#获取新的镜像"></a> 获取新的镜像</h5>
<blockquote>
<p>当我们在本地主机上使用一个不存在的镜像时 Docker 就会<strong>自动下载</strong>这个镜像。</p>
<p>如果我们想预先下载这个镜像，我们可以使用 <code>docker pull</code> 命令来下载它</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 如果不指定一个镜像的版本标签, 将默认使用 ubuntu:latest 镜像</span></span><br><span class="line">docker pull ubuntu</span><br></pre></td></tr></table></figure>
<h5 id="查找远端镜像"><a class="markdownIt-Anchor" href="#查找远端镜像"></a> 查找远端镜像</h5>
<blockquote>
<p>比如我们需要一个httpd的镜像来作为我们的web服务。</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker search httpd</span><br></pre></td></tr></table></figure>
<h5 id="更新镜像"><a class="markdownIt-Anchor" href="#更新镜像"></a> 更新镜像</h5>
<blockquote>
<p>比如本地的 ubuntu:16.04 配置了一定环境之后, 想重新弄一个同样镜像tag来配置另一个环境</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 查看本地镜像列表</span></span><br><span class="line">docker images</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 更新镜像</span></span><br><span class="line">docker commit -m="test for update image" -a="wzx" 9361ce633ff1 wzx/ubuntu:v1</span><br></pre></td></tr></table></figure>
<blockquote>
<ul>
<li><strong>-m</strong> : 提交的描述信息</li>
<li><strong>-a</strong>  : 指定镜像作者</li>
<li><strong>e218edb10161</strong> : 容器ID</li>
<li><strong>wzx/ubuntu:v1</strong> : 指定要创建的目标镜像名</li>
</ul>
</blockquote>
<h5 id="删除镜像"><a class="markdownIt-Anchor" href="#删除镜像"></a> 删除镜像</h5>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 停止所有的容器</span></span><br><span class="line">docker stop $(docker ps -a -q)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="comment"># 如果要删除所有容器的话, 需加上:</span></span></span><br><span class="line">docker rm $(docker ps -a -q)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看本地镜像列表</span></span><br><span class="line">docker images</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 删除单个镜像</span></span><br><span class="line">docker rmi [ContainerID]</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 删除所有镜像</span></span><br><span class="line">docker rmi $(docker images -q)</span><br></pre></td></tr></table></figure>

      
    </div>
    <div class="article-footer">
      <!-- <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="http://zhulao.gitee.io/blog/2019/04/20/Docker教程/" title="Docker教程" target="_blank" rel="external">http://zhulao.gitee.io/blog/2019/04/20/Docker教程/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/wzx3" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/blog/images/avatar.jpeg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/wzx3" target="_blank"><span class="text-dark">王竹兴</span><small class="ml-1x"></small></a></h3>
        <div></div>
      </div>
    </figure>
  </div>
</div>
 -->

    </div>
  </article>
  
    
  <section id="comments">
  	
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/blog/2019/04/25/关于BETA-RC-ALPHA-GA等版本号的意义/" title="关于BETA,RC,ALPHA,GA等版本号的意义"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/blog/2019/04/18/GitLab-CI搭建持续集成环境/" title="GitLab-CI搭建持续集成环境"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
    <li class="toggle-toc">
      <a class="toggle-btn collapsed" data-toggle="collapse" href="#collapseToc" aria-expanded="false" title="文章目录" role="button">
        <span>[&nbsp;</span><span>文章目录</span>
        <i class="text-collapsed icon icon-anchor"></i>
        <i class="text-in icon icon-close"></i>
        <span>]</span>
      </a>
    </li>
    
  </ul>
  
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  


</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/wzx3" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="/blog/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/blog/js/plugin.min.js"></script>
<script src="/blog/js/application.js"></script>

    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/blog/',
        CONTENT_URL: '/blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/blog/js/insight.js"></script>





   
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





   






</body>
</html>